package org.alwayssuper.web.controller;

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.alwayssuper.commons.model.R;
import org.alwayssuper.protocol.gbt32960.CommonResponse;
import org.alwayssuper.protocol.gbt32960.G0B;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.alwayssuper.web.endpoint.MessageManager;
import reactor.core.publisher.Mono;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 用于对车辆终端的命令下行
 * @author alwaysSuper
 */
@RestController
@RequestMapping("device")
@RequiredArgsConstructor
public class GBT32960Controller {

    private final MessageManager messageManager;

    @Operation(summary = "0x0B 数据单元加密密钥交换")
    @PostMapping("0x0B")
    public Mono<R<G0B>> G0B(@RequestBody G0B request) {
        return messageManager.requestR(request, G0B.class);
    }

    @Operation(summary = "0x0B 数据单元加密密钥交换模拟测试")
    @PostMapping("0x0B/simulate")
    public Mono<R<CommonResponse>> G0BSimulateTest() {
        // 直接构造G0B对象进行模拟测试
        G0B request = new G0B();
        
        // 设置基础消息字段
        request.setClientId("LFPH2A1B0K1A12345"); // 17位车辆识别码
        request.setEncryptionType(0x01); // 数据单元加密方式
        request.setProtocolVersion(1); // 协议版本
        request.setMessageId(0x0B); // 消息ID
        request.setResponseType(0xFE);
        
        // 设置G0B特有字段
        request.setKeyType(G0B.KEY_TYPE_AES128); // 使用AES128算法
        
        // 构造测试密钥（16字节的AES128密钥）
        byte[] testKey = "TestKey123456789".getBytes(); // 16字节测试密钥
        request.setEncryptionKey(testKey);
        
        // 设置密钥有效期
        LocalDateTime now = LocalDateTime.now();
        request.setEnableTime(now); // 当前时间启用
        request.setExpireTime(now.plusDays(30)); // 30天后失效
        
        return messageManager.sendVehicleControlCommand(request, CommonResponse.class);
    }

}
